local hx__yanzuo = fk.CreateSkill {
  name = "hx__yanzuo",
}


hx__yanzuo:addEffect("active", {
  name = "hx__yanzuo",
  anim_type = "special",
  prompt = "#hx__yanzuo",
  card_num = 1,
  target_num = 0,
  derived_piles = "yanzuo",
  can_use = function(self, player)
    return player:usedSkillTimes(hx__yanzuo.name, Player.HistoryTurn) < 1 + player:getMark("@hx__zuyin")
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    player:addToPile(hx__yanzuo.name, effect.cards, true, hx__yanzuo.name, player)
    if player.dead or #player:getPile(hx__yanzuo.name) == 0 then return end
    local cards = table.filter(room:getBanner(hx__yanzuo.name), function (id)
      return table.find(player:getPile(hx__yanzuo.name), function (id2)
        return Fk:getCardById(id).name == Fk:getCardById(id2).name
      end) ~= nil
    end)
    if #cards > 0 then
      local use = room:askToUseRealCard(player, {
        pattern = cards,
        skill_name = hx__yanzuo.name,
        prompt = "#hx__yanzuo-ask",
        extra_data = {
          bypass_times = true,
          expand_pile = cards,
        },
        skip = true,
      })
      if use then
        local card = Fk:cloneCard(use.card.name)
        card.skillName = hx__yanzuo.name
        if Fk:getCardById(effect.cards[1]).name == use.card.name then
          player:drawCards(1, hx__yanzuo.name)
        end
        if use.card.type == Card.TypeBasic then
          room:addPlayerMark(player, "hx__yanzuo-basic", 1)
        end
        room:useCard{
          card = card,
          from = player,
          tos = use.tos,
          extraUse = true,
        }
        if use.card.type == Card.TypeTrick then
          local car = room:getCardsFromPileByRule(".|.|.|.|.|trick")
          if #car > 0 then
            room:obtainCard(player, car[1], false, fk.ReasonDraw, player, hx__yanzuo.name)
          end
        end
      end
    end
  end,
})

hx__yanzuo:addEffect(fk.DamageInflicted, {
  can_trigger = function(self, event, target, player, data)
    local cards = player:getCardIds("he")
    return target == player and player:hasSkill(hx__yanzuo.name) and #cards > 0 and
    player:usedSkillTimes(hx__yanzuo.name, Player.HistoryTurn) < 1 + player:getMark("@hx__zuyin")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local car = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      prompt = "#hx__yanzuo",
      skill_name = hx__yanzuo.name,
      cancelable = false,
    })
    player:addToPile(hx__yanzuo.name, car, true, hx__yanzuo.name, player)
    if player.dead or #player:getPile(hx__yanzuo.name) == 0 then return end
    local cards = table.filter(room:getBanner(hx__yanzuo.name), function (id)
      return table.find(player:getPile(hx__yanzuo.name), function (id2)
        return Fk:getCardById(id).name == Fk:getCardById(id2).name
      end) ~= nil
    end)
    if #cards > 0 then
      local use = room:askToUseRealCard(player, {
        pattern = cards,
        skill_name = hx__yanzuo.name,
        prompt = "#hx__yanzuo-ask",
        extra_data = {
          bypass_times = true,
          expand_pile = cards,
        },
        skip = true,
      })
      if use then
        local card = Fk:cloneCard(use.card.name)
        card.skillName = hx__yanzuo.name
        if Fk:getCardById(car[1]).name == use.card.name then
          player:drawCards(1, hx__yanzuo.name)
        end
        if use.card.type == Card.TypeBasic then
          room:addPlayerMark(player, "hx__yanzuo-basic", 1)
        end
        room:useCard{
          card = card,
          from = player,
          tos = use.tos,
          extraUse = true,
        }
        if use.card.type == Card.TypeTrick then
          local carr = room:getCardsFromPileByRule(".|.|.|.|.|trick")
          if #carr > 0 then
            room:obtainCard(player, carr[1], false, fk.ReasonDraw, player, hx__yanzuo.name)
          end
        end
      end
    end
  end,
})

hx__yanzuo:addEffect(fk.CardUsing, {
  is_delay_effect = true,
  anim_type = "offensive",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:getMark("hx__yanzuo-basic") > 0 and player:hasSkill(hx__yanzuo.name) then
      return data.card.type == Card.TypeBasic
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "hx__yanzuo-basic", 0)
    data.additionalDamage = (data.additionalDamage or 0) + 1
    data.additionalRecover = (data.additionalRecover or 0) + 1
  end,
})

hx__yanzuo:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  if room:getBanner(hx__yanzuo.name) == nil then
    local ids = {}
    for _, name in ipairs(Fk:getAllCardNames("bt", false, true)) do
      table.insert(ids, room:printCard(name).id)
    end
    room:setBanner(hx__yanzuo.name, ids)
  end
end)


return hx__yanzuo
